<?php
namespace Home\Controller;
use Think\Controller;


class SupplylistController extends Controller {


   //发布供应物品信息
	function  publishSupply(){

		$supply=M("Supplylist");

		$time=$_POST["time"];
    $timeinterval=$_POST["timeinterval"];
		$goodname=$_POST["goodname"];
    $price=$_POST["price"];
		$minnum=$_POST["minnum"];
		$desc=$_POST["desc"];
		$goodsclassifyid=$_POST["goodsclassifyid"];
	   $userid=$_POST["userid"];
	   $addrid=$_POST["addrid"];
     $goodid=$_POST["goodid"];

	   $data["supplylist_time"]=$time;
     $data["supplylist_timeinterval"]=$timeinterval;
		 $data["supplylist_goodsname"]=$goodname;
     $data["supplylist_price"]=$price;
		 $data["supplylist_minnumber"]=$minnum;
		 $data["supplylist_desc"]=$desc;
		 $data["goodsclassify_id"]=$goodsclassifyid;
		 $data["user_id"]=$userid;
		 $data["address_id"]=$addrid;
     $data["goods_id"]=$goodid;

		 $result=$supply->add($data);
		 
		 if ($result) {
		     $this->ajaxReturn(array(
		        "code"=>200,
		        "message"=>"发布成功!",
            "result"=>$result
		        ),"JSON");
		 }
		 else{
		     $this->ajaxReturn(array(
		        "code"=>500,
		        "message"=>"发布失败！"
		        ),"JSON");
		 }
	}

//上传物品照片
  function uploadimage(){

      //将所有选的图片上传到服务器
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize   =     3145728 ;// 设置附件上传大小
      $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->rootPath  =     './images/Goodsimages/'; // 设置附件上传根目录
      $upload->savePath  =     ''; // 设置附件上传（子）目录
      // 上传文件 
      $info   =   $upload->upload();
      if(!$info) {// 上传错误提示错误信息
          $this->error($upload->getError());
      }else{
      // 上传成功
          $this->success('上传成功！');
        
          $supply=M("Supplylist");
          $data["user_id"]=$_POST["userid"];
          $data["supplylist_id"]=$_POST["supplyid"];

          foreach($info as $file){
          $t=$file['savepath'].$file['savename'].$t;
          }
          $data["supplylist_image"]=$t;
          $result=$supply->save($data);      
            if ($result) {
              $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"修改图像成功！"
                ),"JSON");
            }else{
              $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"修改图像失败！"
                ),"JSON");
            }
      }
        
 
  }


  //查询供应表里所有信息
  public function QuerySupply(){
     //供应表
       $supply=M("Supplylist");
       $data=$supply
       ->field("t_user.user_nick,t_address.address_name,supplylist_goodsname,supplylist_time,supplylist_desc,supplylist_image,supplylist_minnumber")
       ->join("t_user on t_supplylist.user_id =t_user.user_id ")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->select();
       if ($data) {
          $this->ajaxReturn(array("code"=>200,"data"=>$data),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"data"=>NULL),"JSON");
       }
    }

   ////我的供应

    function mysupply(){

    	$supply=M("Supplylist");
    	$userid=$_POST["userid"];
    	$result=$supply
       ->field("t_supplylist.susupplylist_num,t_address.address_name,user_id,t_supplylist.supplylist_goodsname,t_supplylist.supplylist_price,t_supplylist.supplylist_timeinterval,t_supplylist.supplylist_time,
        t_supplylist.supplylist_desc,t_supplylist.supplylist_id,t_supplylist.supplylist_image,t_supplylist.supplylist_minnumber")
       ->join("t_address on t_supplylist.address_id=t_address.address_id")
       ->where("user_id='$userid'")->select();

         if ($result) {
              $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "result"=>$result        ###### 在ios端解析出result中的时间，当供应时间与当前时间相差24小时则移到（我的供应完成中）
                ),"JSON");
            }else{
              $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"查询失败！"
                ),"JSON");
            } 
    }
	

   //按货品id查询所有的需求信息
  function  GoodsIdSupplylist(){

      $supply=M("Supplylist");
       $goodsid=$_POST["goodsid"];
       $result=$supply
       ->where("goods_id='$goodsid'")
       ->select();
          if ($result) {
        $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "data"=>$result
            ),"JSON");
        }
        else{
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"没有相应数据！"
            ),"JSON");
        }


  }

  //按id查询所有的需求信息
  function  SupplylistId(){

      $supply=M("Supplylist");
       $supplyid=$_POST["supplyid"];
       $result=$supply->where("supplylist_id='$supplyid'")->select();
          if ($result) {
        $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "data"=>$result
            ),"JSON");
        }
        else{
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"没有相应数据！"
            ),"JSON");
        }


  }

   //计算浏览次数
  function  supplyNum(){

    $supply=M("Supplylist");
    $supplyid=$_POST["supplyid"];
    // $data["supplylist_id"]=$supplyid;
    // $data["susupplylist_num"]=$data["susupplylist_num"]+1;
    $result=$supply->where("supplylist_id=$supplyid")->setInc('susupplylist_num');
     if ($result) {
      $this->ajaxReturn(array(
              "code"=>200,
              "message"=>"修改成功！"
          ),"JSON");
      }
      else{
        $this->ajaxReturn(array(
                "code"=>500,
                "message"=>"没有相应数据！"
          ),"JSON");
      }
  }



}